﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLVatTu.Class;

namespace QLVatTu.InPhieu
{
    public partial class frmTaoPhieuNhapKho : Form
    {
        KetNoiDB cn = new KetNoiDB();

        public frmTaoPhieuNhapKho()
        {
            InitializeComponent();
        }

        private void btnXem_Click(object sender, EventArgs e)
        {
            frmInPhieuNhapKho frm = new frmInPhieuNhapKho();
            List<PhieuNhapKho> list = new List<PhieuNhapKho>();

            //string Ngay = "";
            //string Thang = "";
            //string Nam = "";
            //string NguoiGiaoHang = "";
            //string DonVi = "";
            //string DiaChi = "";
            //string CongTrinh = "";
            //string TenHangMuc = "";
            //string TenKho = "";

            //string STT = "";
            //string Kho = "";
            //string MaVT = "";
            //string TenVatTu = "";
            //string DVT = "";
            //string HangMuc = "";
            //string SL = "";
            //string Gia = "";
            //string Tien = "";

            //if (cmbNgay.Text.Trim() != "")
            //{
            //    Ngay = cmbNgay.Text.Trim();
            //}

            //if (cmbThang.Text.Trim() != "")
            //{
            //    Thang = cmbThang.Text.Trim();
            //}

            //Nam = nudNam.Value.ToString();

            //if (cmbNguoiGiaoHang.Text.Trim() != "")
            //{
            //    NguoiGiaoHang = cmbNguoiGiaoHang.Text.Trim();
            //}

            //if (cmbDonVi.Text.Trim() != "")
            //{
            //    DonVi = cmbDonVi.Text.Trim();
            //}

            //DiaChi = txtDiaChi.Text.Trim();

            //if (cmbCongTrinh.Text.Trim() != "")
            //{
            //    CongTrinh = cmbCongTrinh.Text.Trim();
            //}

            //TenHangMuc = txtHangMuc.Text.Trim();

            //if (cmbKho.Text.Trim() != "")
            //{
            //    TenKho = cmbKho.Text.Trim();
            //}

            foreach (DataGridViewRow row in dgvVatTu.Rows)
            {
                //PhieuNhapKho fnk = new PhieuNhapKho();
                //fnk.Ngay = Ngay;
                //fnk.Thang = Thang;
                //fnk.Nam = Nam;
                //fnk.NguoiGiaoHang = NguoiGiaoHang;
                //fnk.DonVi = DonVi;
                //fnk.DiaChi = DiaChi;
                //fnk.CongTrinh = CongTrinh;
                //fnk.TenHangMuc = TenHangMuc;
                //fnk.TenKho = TenKho;

                //if (row.Cells[0].Value != null) STT = row.Cells[0].Value.ToString();
                //if (row.Cells[1].Value != null) Kho = row.Cells[1].Value.ToString();
                //if (row.Cells[2].Value != null) MaVT = row.Cells[2].Value.ToString();
                //if (row.Cells[3].Value != null) TenVatTu = row.Cells[3].Value.ToString();
                //if (row.Cells[4].Value != null) DVT = row.Cells[4].Value.ToString();
                //if (row.Cells[5].Value != null) HangMuc = row.Cells[5].Value.ToString();
                //if (row.Cells[6].Value != null) SL = row.Cells[6].Value.ToString();
                //if (row.Cells[7].Value != null) Gia = row.Cells[7].Value.ToString();
                //if (row.Cells[8].Value != null) Tien = row.Cells[8].Value.ToString();

                //fnk.STT = STT;
                //fnk.Kho = Kho;
                //fnk.MaVT = MaVT;
                //fnk.TenVatTu = TenVatTu;
                //fnk.DVT = DVT;
                //fnk.HangMuc = HangMuc;
                //fnk.SL = SL;
                //fnk.Gia = Gia;
                //fnk.Tien = Tien;

                //list.Add(fnk);
            }
            frm.ShowDialog();
        }

        private void loadData()
        {
            cmbNguoiGiaoHang.DataSource = cn.getTable("SELECT [MaNhanVien],[TenNhanVien] FROM [tbl_NhanVien]");
            cmbNguoiGiaoHang.DisplayMember = "TenNhanVien";
            cmbNguoiGiaoHang.ValueMember = "MaNhanVien";

            cmbDonVi.DataSource = cn.getTable("SELECT [MaDoiTac],[TenDoiTac] FROM [tbl_DoiTac] WHERE [NhaCungCap] = 1");
            cmbDonVi.DisplayMember = "TenDoiTac";
            cmbDonVi.ValueMember = "MaDoiTac";

            cmbCongTrinh.DataSource = cn.getTable("SELECT [MaCongTrinhDuAn],[TenCongTrinhDuAn] FROM [tbl_CongTrinhDuAn]");
            cmbCongTrinh.DisplayMember = "TenCongTrinhDuAn";
            cmbCongTrinh.ValueMember = "MaCongTrinhDuAn";

            cmbKho.DataSource = cn.getTable("SELECT [MaKho],[TenKho] FROM [tbl_Kho] WHERE [TrangThai]=1");
            cmbKho.DisplayMember = "TenKho";
            cmbKho.ValueMember = "MaKho";

            load_dgvVatTu();
        }

        private void load_dgvVatTu()
        {
            DataGridViewComboBoxColumn dgvcmbKho = (DataGridViewComboBoxColumn)dgvVatTu.Columns[1];
            dgvcmbKho.DataSource = cn.getTable("SELECT [MaKho],[TenKho] FROM [tbl_Kho] WHERE [TrangThai]=1 ORDER BY [TenKho] ASC");
            dgvcmbKho.DisplayMember = "TenKho";
            dgvcmbKho.ValueMember = "MaKho";

            DataGridViewComboBoxColumn dgvcmbVatTu = (DataGridViewComboBoxColumn)dgvVatTu.Columns[2];
            dgvcmbVatTu.DataSource = cn.getTable("SELECT [MaVatTu],[TenVatTu] FROM [tbl_VatTu] ORDER BY [MaVatTu] ASC");
            dgvcmbVatTu.DisplayMember = "MaVatTu";
            dgvcmbVatTu.ValueMember = "MaVatTu";
        }

        private void frmTaoPhieuNhapKho_Load(object sender, EventArgs e)
        {
            if (cn.KetNoi())
            {
                loadData();
            }
            else
            {
                MessageBox.Show("Kết nối CSDL thất bại!");
                this.Close();
            }
        }

        private void cmbDonVi_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbDonVi.Items.Count > 0)
            {
                txtDiaChi.Text = cn.LayDuLieu("SELECT [DiaChi] FROM [tbl_DoiTac] WHERE [MaDoiTac] = '" + cmbDonVi.SelectedValue + "'");
            }
        }

        private void dgvVatTu_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
//            if (dgvVatTu.RowCount > 0 && e.RowIndex != -1)
//            {
//                if (e.ColumnIndex == 1 && dgvVatTu.Rows[e.RowIndex].Cells[1].Value != null)
//                {

//                }

//                if (e.ColumnIndex == 2 && dgvVatTu.Rows[e.RowIndex].Cells[2].Value != null)
//                {

//                }

//                if (e.ColumnIndex == 4 && dgvTonKho.Rows[e.RowIndex].Cells[1].Value != null && dgvTonKho.Rows[e.RowIndex].Cells[4].Value != null)
//                {
//                    string sql = "";
//                    sql = @"
//                                        SELECT     dbo.tbl_VatTu.MaVatTu, dbo.tbl_VatTu.TenVatTu as VatTu, dbo.tbl_DonViTinhGoc.TenDonViTinhGoc as DVT, dbo.tbl_LuuKho.MaKho, dbo.tbl_LuuKho.SoLuong as TonKho
//                                        FROM         dbo.tbl_VatTu INNER JOIN
//                                                                dbo.tbl_DonViTinhGoc ON dbo.tbl_VatTu.MaDonViTinhGoc = dbo.tbl_DonViTinhGoc.MaDonViTinhGoc INNER JOIN
//                                                                dbo.tbl_LuuKho ON dbo.tbl_VatTu.MaVatTu = dbo.tbl_LuuKho.MaVatTu INNER JOIN
//                                                                dbo.tbl_Kho ON dbo.tbl_LuuKho.MaKho = dbo.tbl_Kho.MaKho
//                                                "
//                    + " WHERE 1=1 AND dbo.tbl_VatTu.MaVatTu = '" + dgvTonKho.Rows[e.RowIndex].Cells[1].Value + "' AND dbo.tbl_LuuKho.MaKho = '" + dgvTonKho.Rows[e.RowIndex].Cells[4].Value + "'";

//                    DataTable dt = new DataTable();
//                    dt = cn.getTable(sql);
//                    if (dt.Rows.Count > 0)
//                    {
//                        string stt = dgvTonKho.Rows[e.RowIndex].Cells[0].Value.ToString();
//                        DataGridViewTextBoxCell txtSTT = (DataGridViewTextBoxCell)dgvTonKho.Rows[e.RowIndex].Cells["STT"];
//                        txtSTT.Value = stt;
//                        DataGridViewTextBoxCell txtMaVatTu = (DataGridViewTextBoxCell)dgvTonKho.Rows[e.RowIndex].Cells["MaVatTu"];
//                        txtMaVatTu.Value = dt.Rows[0]["MaVatTu"].ToString();
//                        DataGridViewTextBoxCell txtVatTu = (DataGridViewTextBoxCell)dgvTonKho.Rows[e.RowIndex].Cells["VatTu"];
//                        txtVatTu.Value = dt.Rows[0]["VatTu"].ToString();
//                        DataGridViewTextBoxCell txtDVT = (DataGridViewTextBoxCell)dgvTonKho.Rows[e.RowIndex].Cells["DVT"];
//                        txtDVT.Value = dt.Rows[0]["DVT"].ToString();
//                        DataGridViewComboBoxCell cmbKho = (DataGridViewComboBoxCell)dgvTonKho.Rows[e.RowIndex].Cells["Kho"];
//                        cmbKho.Value = dt.Rows[0]["MaKho"].ToString();
//                        DataGridViewTextBoxCell txtTonKho = (DataGridViewTextBoxCell)dgvTonKho.Rows[e.RowIndex].Cells["TonKho"];
//                        txtTonKho.Value = dt.Rows[0]["TonKho"].ToString();
//                    }
//                    else
//                    {
//                        DataGridViewTextBoxCell txtTonKho = (DataGridViewTextBoxCell)dgvTonKho.Rows[e.RowIndex].Cells["TonKho"];
//                        txtTonKho.Value = 0;
//                    }
//                }
//            }
        }
    }
}
